import { reactive, onDeactivated, onActivated, Ref } from 'vue'

export const useLastPositon = (scrollElement: Ref<HTMLElement | undefined>) => {
  const refLastPosition = reactive({ x: 0, y: 0 })
  onActivated(() => {
    console.log(scrollElement.value)
    if (!(refLastPosition.x === 0 && refLastPosition.y === 0)) {
      scrollElement.value?.scrollTo({
        top: refLastPosition.y,
        left: refLastPosition.x
      })
    }
  })
  onDeactivated(() => {
    refLastPosition.x = scrollElement.value?.scrollLeft ?? 0
    refLastPosition.y = scrollElement.value?.scrollTop ?? 0
    console.log(refLastPosition)
  })
}
